<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:room:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:room:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:room:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:room:export']"
        >导出</el-button>
      </el-col>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="el-form-search">
                        <el-form-item label="房间号" prop="roomNo" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.roomNo"
                                    placeholder="请输入房间号"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="房间类型" prop="roomType" class="el-form-search-item">
                            <el-select v-model="queryParams.roomType" placeholder="请选择房间类型：单间、  标间 、特殊房间" clearable size="mini">
                                <el-option
                                        v-for="dict in dict.type.room_type"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="楼层位置" prop="roomFloor" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.roomFloor"
                                    placeholder="请输入楼层位置"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
            <el-form-item class="el-form-search-item">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
    </el-row>

    <el-table :height="tableHeight" v-loading="loading" :data="roomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="#" type="index" width="50" align="center">
            <template scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
            </template>
        </el-table-column>
      <el-table-column label="房间id" align="center" prop="roomId" width="180"
                       :key="columns[0].key"
                       v-if="columns[0].visible"/>
      <el-table-column label="房间号" align="center" prop="roomNo"></el-table-column>

      <el-table-column
          label="房间类型"
          align="center"
          prop="roomType"
          :formatter="roomTypeFormatter"
      >

        <template slot-scope="scope">
          <dict-tag :options="dict.type.room_type" :value="scope.row.roomType"/>
        </template>
      </el-table-column>

      <el-table-column
          label="楼层位置"
          align="center"
          prop="roomFloor"
          :formatter="roomFloorFormatter"
      />

      <el-table-column label="房间容量"  align="center" prop="roomCapacity" />

      <el-table-column label="房间图片" align="center" prop="roomImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.roomImg" :width="30" :height="30"/>
        </template>
      </el-table-column>

      <el-table-column
          label="房间状态"
          align="center"
          prop="roomStatus"
          :formatter="roomStatusFormatter"
      />

      <el-table-column label="校区id" align="center" prop="campusId" :show-tooltip-when-overflow="true"/>
      <el-table-column label="备注" align="center" prop="remark" :show-tooltip-when-overflow="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:room:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:room:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房间管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false" v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房间号" prop="roomNo">
          <el-input v-model="form.roomNo" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="房间类型" prop="roomType">
          <el-select
              v-model="form.roomType"
              placeholder="请输入房间类型"
              clearable
              :style="{ width: '100%' }"
          >
            <el-option
                v-for="(item, index) in roomTypeOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
            ></el-option>

          </el-select>
        </el-form-item>
        <el-form-item label="楼层位置" prop="roomFloor">
          <el-select
              v-model="form.roomFloor"
              placeholder="请输入楼层位置"
              clearable
              :style="{ width: '100%' }"
          >
            <el-option
                v-for="(item, index) in roomFloorOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
            ></el-option>

          </el-select>
        </el-form-item>
        <el-form-item label="床位容量" prop="roomCapacity">
          <el-input v-model="form.roomCapacity" placeholder="请输入房间床位容量" />
        </el-form-item>
        <el-form-item label="房间图片">
          <image-upload v-model="form.roomImg"/>
        </el-form-item>
        <el-form-item label="房间状态">
          <el-select
              v-model="form.roomStatus"
              placeholder="请输入楼层位置"
              clearable
              :style="{ width: '100%' }"
          >
            <el-option
                v-for="(item, index) in roomStatusOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="校区id" prop="campusId">
          <el-input v-model="form.campusId" placeholder="请输入校区id" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRoom, getRoom, delRoom, addRoom, updateRoom } from "@/api/system/room";

export default {
  name: "Room",
  dicts: ['room_type'],
  data() {
    return {
      // 表格高度
      tableHeight: document.documentElement.clientHeight - 180,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间管理表格数据
      roomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: "create_time",
        isAsc: "desc",
        roomNo: null,
        roomType: null,
        roomStatus: null,
        roomFloor: null,
      },
      // 表单参数
      form: {},
      columns: [
        { key: 0, label: `房间id`, visible: true },
        { key: 1, label: `房间号`, visible: true },
        { key: 2, label: `房间类型`, visible: true },
        { key: 3, label: `楼层位置`, visible: true },
        { key: 4, label: `床位容量`, visible: true },
        { key: 5, label: `房间图片`, visible: true },
        { key: 6, label: `房间状态`, visible: true },
        { key: 7, label: `校区id`, visible: true },
        { key: 8, label: `备注`, visible: true },
      ],
      // 表单校验
      rules: {
        roomId: [
          { required: true, message: "房间id不能为空", trigger: "blur" }
        ],
        roomNo: [
          { required: true, message: "房间号不能为空", trigger: "blur" }
        ],
        roomType: [
          { required: true, message: "房间类型不能为空", trigger: "change" }
        ],
        roomFloor: [
          { required: true, message: "楼层位置不能为空", trigger: "blur" }
        ],
        roomCapacity: [
          { required: true, message: "房间床位容量不能为空", trigger: "blur" }
        ],
        roomStatus: [
          { required: true, message: "房间状态不能为空", trigger: "blur" }
        ],
      },
      roomFloorOptions:[
        {
          label: "一楼",
          value: "1",
        },{
          label: "二楼",
          value: "2",
        },{
          label: "三楼",
          value: "3",
        },{
          label: "四楼",
          value: "4",
        },{
          label: "五楼",
          value: "5",
        },{
          label: "六楼",
          value: "6",
        },
      ],
      roomStatusOptions:[
        {
          label: "可使用",
          value: "0",
        },{
          label: "不可用",
          value: "1",
        },{
          label: "待清洁",
          value: "2",
        },{
          label: "维修中",
          value: "3",
        },{
          label: "已住满",
          value: "4",
        },
      ],
      roomTypeOptions:[
        {
          label: "单间",
          value: "0",
        },{
          label: "标间",
          value: "1",
        },{
          label: "特殊房间",
          value: "2",
        },
      ]

    };
  },
  created() {
    this.getList();
  },
  methods: {
    roomFloorFormatter(row, column , value) {
      if (row.roomFloor === "1") {
        return "一楼";
      } else if (row.roomFloor === "2") {
        return "二楼";
      } else if (row.roomFloor === "3") {
        return "三楼";
      } else if (row.roomFloor === "4") {
        return "四楼";
      }else if (row.roomFloor === "5"){
        return "五楼";
      }else if (row.roomFloor === "6"){
        return "六楼";
      }else {
        return "未知";
      }
    },

    roomStatusFormatter(row, column , value) {
      if (row.roomStatus === "0") {
        return <el-tag type="success">可使用</el-tag>;
      } else if (row.roomStatus === "1") {
        return <el-tag type="info">不可用</el-tag>;
      } else if (row.roomStatus === "2") {
        return <el-tag type="warning">待清洁</el-tag>;
      } else if(row.roomStatus === "3") {
        return <el-tag type="danger">维修中</el-tag>;
      }else if (row.roomStatus === "4"){
        return <el-tag type="primary">已住满</el-tag>;
      }else{
        return "未知";
      }
    },
    roomTypeFormatter(row, column){
      if (row.roomType === "0"){
        return <span>单间</span>
      }else if (row.roomType === "1"){
        return <span>标间</span>
      }else if (row.roomType === "2")
        return <span>特殊房间</span>
    },

    /** 查询房间管理列表 */
    getList() {
      this.loading = true;
      listRoom(this.queryParams).then(response => {
        this.roomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        roomId: null,
        roomNo: null,
        roomType: null,
        roomFloor: null,
        roomCapacity: null,
        roomImg: null,
        roomStatus: "0",
        campusId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roomId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房间管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const roomId = row.roomId || this.ids
      getRoom(roomId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房间管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.roomId != null) {
            updateRoom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roomIds = row.roomId || this.ids;
      this.$modal.confirm('是否确认删除记录？').then(function() {
        return delRoom(roomIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/room/export', {
        ...this.queryParams
      }, `room_${new Date().getTime()}.xlsx`)
    },

  },
};
</script>
